﻿using System.Collections.Generic;
namespace DarrenSoft.Facade
{
    using DarrenSoft.Domain.DomainBase;
    using DarrenSoft.Domain.Extends;
    using DarrenSoft.DataAccessLayer.Repositories;
    using DarrenSoft.DataAccessLayer.Interfaces;
    public  class BaseFacade<T>:IBaseFacade<T> where T:Entity,new()
    {
        public IRepository<T> Repository = null;
        private string dalName =string.Empty;
        public BaseFacade() {                    
             Repository = new Repository<T>(typeof(T).Name,"ID");
        }
        public BaseFacade(string tableName,string primaryKey) {

            this.dalName = tableName;
            if (string.IsNullOrEmpty(dalName))
            {
                this.dalName = GetType().Name;
            }
            Repository = new Repository<T>();
        }
        /// <summary>
        /// 通过主键返回对象
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public T Get(object id)
        {
            return Repository.Get(id);
        }     
        /// <summary>
        /// 保存对象
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public object Insert(T entity)
        {
            return Repository.Insert(entity);
        }

        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="model">实体</param>
        public bool Update(T entity, string primaryKeyValue)
        {
           return Repository.Update(entity,primaryKeyValue);
        }    
        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="model">实体</param>
        public void Delete(object id)
        {
          Repository.Delete(id);
        }
        /// <summary>
        /// 获取所有记录
        /// </summary>
        /// <returns></returns>
        public IList<T> GetAll()
        {
            return Repository.GetAll();
        }
        public List<T> Find(out int recordsCount,string condition, PagerBase info)
        {
            return Repository.Find(out recordsCount,condition, info);
        }
    }
}
